package com.zxl.demo;

import com.zxl.demo.model.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author zxl
 * @version 1.0
 * @date 2021/10/30 21:45
 */
@WebServlet(name = "TestServlet",value = "/TestServlet")
public class TestServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //字符编码写在最前面
        resp.setHeader("Content-type", "text/html;charset=UTF-8");

        PrintWriter writer = resp.getWriter();
        writer.write("哈哈哈");

        ServletContext servletContext = this.getServletContext();

        //获得初始化参数
        String url = servletContext.getInitParameter("url");
        writer.write(url);
        writer.write("\n");

        User user = (User) servletContext.getAttribute("user");
        List<User> userList = (List<User>) servletContext.getAttribute("userList");

        writer.write(user.toString());
        writer.write("\n");
        writer.write("\n");

        for (int i = 0; i < userList.size(); i++) {
            String s = userList.get(i).toString();
            writer.write(s);
            writer.write("\n");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
